<template>
  <div class="project-application-container">
    <!-- 主容器布局 -->
    <div class="main-layout">
      <!-- 左侧内容容器 -->
      <div class="left-container">
        <!-- 主标题 -->
        <div class="header-section">
          <h1 class="main-title">请选择你要办理的项目类型</h1>
        </div>

        <!-- 项目类型卡片 -->
        <div class="card-container">
          <!-- 独立选址项目卡片 -->
          <el-card class="project-card" shadow="hover" @click="handleSelect('independent')">
            <div class="card-content">
              <div class="card-icon">
                <el-icon :size="60"><Location /></el-icon>
              </div>
              <h2 class="card-title">独立选址项目</h2>
              <p class="card-desc">需要独立土地进行建设的生产性项目</p>
            </div>
          </el-card>

          <!-- 租赁厂房项目卡片 -->
          <el-card class="project-card" shadow="hover" @click="handleSelect('rental')">
            <div class="card-content">
              <div class="card-icon">
                <el-icon :size="60"><OfficeBuilding /></el-icon>
              </div>
              <h2 class="card-title">租赁厂房项目</h2>
              <p class="card-desc">租用现有厂房进行生产的项目</p>
            </div>
          </el-card>
        </div>
      </div>

      <!-- 右侧容器 - 我已申请的 -->
      <div class="right-container">
        <div class="applied-projects">
          <h3 class="applied-title">我已申请的</h3>
          <el-divider />
          
          <div class="applied-list">
            <el-empty description="暂无申请记录" v-if="appliedProjects.length === 0" />
            
            <div class="project-item" v-for="project in appliedProjects" :key="project.id">
              <div class="project-info">
                <h4 class="project-name">{{ project.name }}</h4>
                <div class="project-meta">
                  <el-tag :type="getStatusTagType(project.status)" size="small">
                    {{ project.status }}
                  </el-tag>
                  <span class="project-date">{{ project.date }}</span>
                </div>
              </div>
              <el-button type="text" size="small" @click="viewProject(project.id)">
                查看详情
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref,computed } from 'vue'
import { useRouter } from 'vue-router'
import { Location, OfficeBuilding } from '@element-plus/icons-vue'
import { useProjectStore } from '@/stores/project' 

const router = useRouter()
const projectStore = useProjectStore()

// 处理项目类型选择
const handleSelect = (type) => {
  // 根据选择类型跳转到对应申请页面
  switch(type) {
    case 'independent':
      router.push('/independent')
      break
    case 'rental':
      router.push('/rental')
      break
    default:
      break
  }
}

// 修改后的函数，同时获取征地报批和竣工验收项目数据
const appliedProjects = computed(() => {
  // 合并两种项目数据
  const combinedProjects = [
    ...projectStore.landApprovalProjects,
    ...projectStore.completionProjects
  ].map(project => ({
    id: project.id,
    name: project.projectName,
    type: project.type === 'rental' ? '租赁厂房' : '独立选址', // 添加项目类型显示
    status: project.status || '审核中',
    date: project.signDate || new Date().toLocaleDateString(),
    state: project.state // 保留项目状态字段
  }))
  
  // 按日期降序排序
  return combinedProjects.sort((a, b) => new Date(b.date) - new Date(a.date))
})

// 获取状态标签类型
const getStatusTagType = (status) => {
  const map = {
    '审核中': 'warning',
    '已通过': 'success',
    '已驳回': 'danger',
    '已完成': 'info'
  }
  return map[status] || ''
}

// 查看项目详情
const viewProject = (id) => {
  router.push({
    path: '/clientprojectdetail',
    query: {
      projectId: id // 传递项目ID
    }
  })
}
</script>

<style scoped>
.project-application-container {
  width: 100%;
  max-width: 1400px;
  margin: 0 auto;
  padding: 40px 20px;
  min-height: 80vh;
}

.main-layout {
  display: flex;
  gap: 40px;
}

.left-container {
  flex: 1;
  max-width: 800px;
}

.right-container {
  width: 350px;
  margin-right: 20px;
}

.header-section {
  text-align: left;
  margin-bottom: 40px;
}

.main-title {
  font-size: 32px;
  color: #333;
  font-weight: 500;
  letter-spacing: 1px;
  margin: 0;
}

.card-container {
  display: flex;
  gap: 40px;
  flex-wrap: wrap;
}

.project-card {
  width: 320px;
  height: 280px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 12px;
}

.project-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.card-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 20px;
  text-align: center;
}

.card-icon {
  margin-bottom: 24px;
  color: #409EFF;
}

.card-title {
  font-size: 24px;
  color: #333;
  margin-bottom: 16px;
}

.card-desc {
  font-size: 16px;
  color: #666;
  line-height: 1.6;
}

/* 右侧容器样式 */
.applied-projects {
  background-color: white;
  border-radius: 12px;
  padding: 25px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 40px;
}

.applied-title {
  font-size: 20px;
  color: #333;
  margin: 0 0 15px 0;
}

.applied-list {
  min-height: 300px;
}

.project-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.project-info {
  flex: 1;
}

.project-name {
  margin: 0 0 8px 0;
  font-size: 16px;
  color: #333;
}

.project-meta {
  display: flex;
  align-items: center;
  gap: 10px;
}

.project-date {
  font-size: 13px;
  color: #999;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .main-layout {
    flex-direction: column;
  }
  
  .right-container {
    width: 100%;
    margin-top: 40px;
  }
  
  .applied-projects {
    position: static;
  }
}

@media (max-width: 768px) {
  .card-container {
    flex-direction: column;
    align-items: center;
  }
  
  .project-card {
    width: 100%;
    max-width: 400px;
  }
  
  .main-title {
    font-size: 28px;
  }
  
  .applied-title {
    font-size: 18px;
  }
}
</style>